package com.flyou.ccnucenter.ui.activity.setting;

import android.app.ProgressDialog;
import android.content.Intent;
import android.text.ClipboardManager;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.e1858.myalertview.AlertView;
import com.e1858.myalertview.OnItemClickListener;
import com.flyou.ccnucenter.Constant;
import com.flyou.ccnucenter.R;
import com.flyou.ccnucenter.ui.activity.BaseActivity1;
import com.flyou.ccnucenter.ui.activity.MainActivity;
import com.flyou.ccnucenter.ui.activity.aboutActivity.AboutActivity;
import com.flyou.ccnucenter.ui.activity.webView.WebViewActivity;
import com.flyou.ccnucenter.utils.AppUtils;
import com.flyou.ccnucenter.utils.DateClearUtils;
import com.flyou.ccnucenter.utils.SPUtils;
import com.flyou.ccnucenter.widget.switchbtn.SwitchButton;
import com.umeng.message.PushAgent;
import com.umeng.update.UmengUpdateAgent;
import com.umeng.update.UpdateStatus;

import org.simple.eventbus.EventBus;

import cn.bmob.v3.BmobUser;

public class SettingActivity extends BaseActivity1 implements View.OnClickListener {
    private RelativeLayout mTitlebarRlBack;
    private TextView mTitlebarTvTitle;
    private AlertView mAlertView;
    private PushAgent mPushAgent;
    private TextView tvCurrentVersion;
    private SwitchButton tbPushSwitch;
    private SwitchButton tbUpdateSwitch;
    private TextView tvFunctionIntroduction;
    private TextView tvAbout;
    private LinearLayout llCheckVersion;
    private TextView tvCheckCurrentVersion;
    private LinearLayout llClearCache;
    private TextView tvClearCacheNumber;
    private LinearLayout llSupportPhone;
    private TextView tvSupportPhone;
    private Button btLogout;


    @Override
    protected void initView() {
        mPushAgent=PushAgent.getInstance(SettingActivity.this);
        setContentView(R.layout.activity_setting);
        mTitlebarRlBack = (RelativeLayout) findViewById(R.id.titlebar_rl_back);
        mTitlebarTvTitle = (TextView) findViewById(R.id.titlebar_tv_title);
        tvCurrentVersion = (TextView) findViewById(R.id.tv_current_version);
        tbPushSwitch = (SwitchButton) findViewById(R.id.tb_push_switch);
        tbUpdateSwitch = (SwitchButton)findViewById(R.id.tb_update_switch);
        tvFunctionIntroduction = (TextView) findViewById(R.id.tv_function_introduction);
        tvAbout = (TextView) findViewById(R.id.tv_about);
        llCheckVersion = (LinearLayout)findViewById(R.id.ll_check_version);
        tvCheckCurrentVersion = (TextView) findViewById(R.id.tv_check_current_version);
        llClearCache = (LinearLayout) findViewById(R.id.ll_clear_cache);
        tvClearCacheNumber = (TextView) findViewById(R.id.tv_clear_cache_number);
        llSupportPhone = (LinearLayout) findViewById(R.id.ll_support_phone);
        tvSupportPhone = (TextView)findViewById(R.id.tv_support_phone);
        btLogout = (Button) findViewById(R.id.bt_logout);

        if (TextUtils.isEmpty(SPUtils.get(SettingActivity.this, "userName", "").toString())) {
            btLogout.setVisibility(View.GONE);
        }


    }

    @Override
    protected void initDate() {
        mTitlebarTvTitle.setText("设置中心");
        tvCurrentVersion.setText("Star在线V" + AppUtils.getVersionName(SettingActivity.this));
        tvCheckCurrentVersion.setText("V" + AppUtils.getVersionName(SettingActivity.this));
        try {
            tvClearCacheNumber.setText(DateClearUtils.getTotalCacheSize(SettingActivity.this));
        } catch (Exception e) {
            e.printStackTrace();
        }


        //        设置推送开关，默认为开
        tbPushSwitch.setChecked((Boolean) SPUtils.get(SettingActivity.this, Constant.IS_AUTO_PUSH, true));
        tbUpdateSwitch.setChecked((Boolean) SPUtils.get(SettingActivity.this, Constant.IS_AUTO_UPDATE, true));
        //        推送开关
        tbPushSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView,
                                         boolean isChecked) {
                SPUtils.put(SettingActivity.this, Constant.IS_AUTO_PUSH, isChecked);
                if (isChecked) {
                    mPushAgent.enable();
                } else {
                    mPushAgent.disable();
                }
            }
        });

        //        进入应用自动提示更新
        tbUpdateSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

            @Override
            public void onCheckedChanged(CompoundButton buttonView,
                                         boolean isChecked) {
                SPUtils.put(SettingActivity.this, Constant.IS_AUTO_UPDATE, isChecked);
            }
        });


    }

    @Override
    protected void setListener() {
        mTitlebarRlBack.setOnClickListener(this);
        tvFunctionIntroduction.setOnClickListener(this);
        tvAbout.setOnClickListener(this);
        llCheckVersion.setOnClickListener(this);
        llClearCache.setOnClickListener(this);
        llSupportPhone.setOnClickListener(this);
        btLogout.setOnClickListener(this);


    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tv_function_introduction:
                Intent fuctionDescIntent = new Intent(SettingActivity.this, WebViewActivity.class);
                fuctionDescIntent.putExtra("title", "功能介绍");
                fuctionDescIntent.putExtra("url", Constant.BASE_URL + Constant.FUNTION_DESC);
                startActivity(fuctionDescIntent);
                break;
            case R.id.tv_about:

                Intent aboutIntent = new Intent(SettingActivity.this, AboutActivity.class);
                startActivity(aboutIntent);
                break;
            case R.id.ll_check_version:
                checkUpdate();
                break;
            case R.id.ll_clear_cache:
                mAlertView = new AlertView("清除缓存", "您确定要清除应用缓存吗？", "取消", null, new String[]{"确定"}, SettingActivity.this, AlertView.Style.Alert, new OnItemClickListener() {
                    @Override
                    public void onItemClick(Object o, int position) {
                        if (position == 0) {
                            clearCache();
                        }
                    }
                });
                mAlertView.show();
                break;
            case R.id.ll_support_phone:
                ClipboardManager clipboardManager = (ClipboardManager) getSystemService(CLIPBOARD_SERVICE);
                clipboardManager.setText("dai王的QQ：553274238");
                Toast.makeText(SettingActivity.this, "QQ已经复制", Toast.LENGTH_SHORT).show();
                break;
            case R.id.bt_logout:
                mAlertView = new AlertView("退出登录", "您确定要退出当前账号吗？", "取消", null, new String[]{"确定"}, SettingActivity.this, AlertView.Style.Alert, new OnItemClickListener() {
                    @Override
                    public void onItemClick(Object o, int position) {
                        if (position == 0) {
                            BmobUser.logOut(SettingActivity.this);   //清除缓存用户对象
                            SPUtils.remove(SettingActivity.this, "userName");
                            SPUtils.remove(SettingActivity.this, "email");
                            SPUtils.remove(SettingActivity.this, "phone");
                            SPUtils.remove(SettingActivity.this, "headImage");
                            SPUtils.remove(SettingActivity.this, "objectId");
                            SPUtils.remove(SettingActivity.this, "grade");
                            SPUtils.remove(SettingActivity.this, "department");
                            btLogout.setVisibility(View.GONE);
                            // 通知主界面更新头像
                            EventBus.getDefault().post("", Constant.UPDATE_HEADIMAGE);
                            Intent intent =new Intent(SettingActivity.this, MainActivity.class);
                            startActivity(intent);

                        }
                    }
                });
                mAlertView.show();
                break;

            case R.id.titlebar_rl_back:
               finish();
                break;
        }
    }
    protected void checkUpdate() {
        final ProgressDialog dialog = ProgressDialog.show(SettingActivity.this, null, "正在检查更新");
        dialog.setCancelable(true);
        UmengUpdateAgent.setDefault();
        UmengUpdateAgent.setUpdateAutoPopup(false);
        UmengUpdateAgent.setUpdateListener(new com.umeng.update.UmengUpdateListener() {

            @Override
            public void onUpdateReturned(int updateStatus, com.umeng.update.UpdateResponse updateInfo) {
                dialog.dismiss();
                switch (updateStatus) {
                    case UpdateStatus.Yes: // has update
                        UmengUpdateAgent.showUpdateDialog(SettingActivity.this, updateInfo);
                        break;
                    case UpdateStatus.No: // has no update
                        Toast.makeText(SettingActivity.this, "当前是最新版本", Toast.LENGTH_SHORT).show();
                        break;
                    case UpdateStatus.NoneWifi: // none wifi
                        Toast.makeText(SettingActivity.this, "没有wifi连接， 只在wifi下更新", Toast.LENGTH_SHORT).show();
                        break;
                    case UpdateStatus.Timeout: // time out
                        Toast.makeText(SettingActivity.this, "检查版本超时", Toast.LENGTH_SHORT).show();
                        break;
                }
            }

        });
        UmengUpdateAgent.forceUpdate(SettingActivity.this);
    }


    /*
    *清除缓存 只清除 内部和sdka下的cache牡蛎
    */
    protected void clearCache() {

        DateClearUtils.cleanInternalCache(SettingActivity.this);
        DateClearUtils.cleanExternalCache(SettingActivity.this);

        try {
            tvClearCacheNumber.setText(DateClearUtils.getTotalCacheSize(SettingActivity.this));
        } catch (Exception e) {
            e.printStackTrace();
        }
        Toast.makeText(SettingActivity.this, "缓存已清空", Toast.LENGTH_SHORT).show();
    }

}
